package com.jadesoft.ciadapter.plugin;

import java.io.IOException;

import javax.servlet.ServletRequest;

import com.jadesoft.ciadapter.library.JdPageHelper;
import com.jadesoft.ciadapter.plugin.base.DefaultPageAction;
import com.jadesoft.ciadapter.plugin.base.Pluggable;
import com.jadesoft.ciadapter.plugin.base.PluginAction;
import com.jadesoft.ciadapter.plugin.base.PluginManager;
import com.ufida.leaf.exception.BusinessException;

import freemarker.template.TemplateException;

public class StabPlugin implements Pluggable {
	ServletRequest request = null ;
	public void init(Object container) {
		if ( ! (container instanceof ServletRequest)){
			throw new BusinessException("invalid container");
		}
		this.request = (ServletRequest)container ;
		
		JdPageHelper hepler = JdPageHelper.getHelper(this.request);
		
		hepler.css_enqueue("single-button/sbutton");
		
		PluginAction script = new DefaultPageAction(this.request){
			public Object exec(Object... args) {
				return getHeadScript();
			}
		};
		PluginManager.addAction("after_html", script,container);

	}
	
	private String getHeadScript(){
		String result = "";
		try {
			result = JdPageHelper.loadTemplate("s-tab/scripts.ftl", null, null, this.request);
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result ;
		
	}
	

}
